package com.yxkj.syh.app.huarong.views;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.syh.app.basic.utils.AppUtil;
import com.syh.app.basic.utils.Tooast;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.bean.ReturnCar;

/**
 * 创建者：SYH
 * 时间：2020/4/24 17:16
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class ReturnCarAppointmentDialog extends Dialog {


    private WindowManager.LayoutParams mLayoutParams;
    private OnClickListener mOnClickListener;
    private Context mContext;

    private TextView tvCancel, tvStartCarTime, tvCarId, tvFrom, tvTo;
    private Button btnConfirm;
    private EditText etRemark;
    private ReturnCar mReturnCar;
    private TextView tvArea;

    public ReturnCarAppointmentDialog(Context context) {
        super(context, R.style.FullDialog);
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_return_car_appointment, (ViewGroup) null);
        tvCancel = dialogView.findViewById(R.id.tvCancel);
        tvStartCarTime = dialogView.findViewById(R.id.tvStartCarTime);
        tvCarId = dialogView.findViewById(R.id.tvCarId);
        tvFrom = dialogView.findViewById(R.id.tvFrom);
        tvTo = dialogView.findViewById(R.id.tvTo);
        etRemark = dialogView.findViewById(R.id.etRemark);
        btnConfirm = dialogView.findViewById(R.id.btnConfirm);
        tvArea = dialogView.findViewById(R.id.tvArea);
        dialogView.findViewById(R.id.llArea).setOnClickListener(v -> {
            if (mOnClickListener != null) {
                mOnClickListener.onArea();
            }
        });

        etRemark.setHint("请输入装货地址");

        tvCancel.setOnClickListener(v -> {
            cancel();
        });
        btnConfirm.setOnClickListener(v -> {
            if (TextUtils.isEmpty(etRemark.getText().toString()) || TextUtils.isEmpty(tvArea.getText().toString())) {
                Tooast.normalInfo("请输入装货地址");
                return;
            }
            cancel();
            if (mOnClickListener != null) {
                mOnClickListener.onConfirm(mReturnCar.getId(), tvArea.getText().toString()+etRemark.getText().toString());
            }
        });

        setContentView(dialogView);
    }

    public ReturnCarAppointmentDialog build(ReturnCar returnCar) {
        this.mReturnCar = returnCar;
        tvStartCarTime.setText(returnCar.getCreateTime());
        tvCarId.setText(returnCar.getCarNo());
        tvFrom.setText(returnCar.getStartArea());
        tvTo.setText(returnCar.getEndArea());
        return this;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.mOnClickListener = onClickListener;
    }

    public void setArea(String area){
        tvArea.setText(area);
    }

    public interface OnClickListener {
        void onConfirm(long id, String remark);

        void onArea();
    }

}
